/**
 @ Name：layui.customTreeSelect 树形下拉框
 @ Author：wwk
 */

layui.define('form', function(exports) { //假如该组件依赖 layui.form
    var $ = layui.$,
        form = layui.form

    //字符常量
    , MOD_NAME = 'customTreeSelect', ELEM = '.layui-customTreeSelect', PNAME = 'custom-select-parent'

    //外部接口
    , customTreeSelect = {
        index: layui.customTreeSelect ? (layui.customTreeSelect.index + 10000) : 0

        //设置全局项
        ,
        set: function(options) {
            var that = this;
            that.config = $.extend({}, that.config, options);
            return that;
        }

        //事件监听
        ,
        on: function(events, callback) {
            return layui.onevent.call(this, MOD_NAME, events, callback);
        }
    }

    //操作当前实例
    , thisIns = function() {
        var that = this,
            options = that.config,
            id = options.id || options.index;

        return {
            reload: function(options) {
                that.reload.call(that, options);
            },
            config: options
        }
    }

    //构造器
    , Class = function(options) {
        var that = this;
        that.index = ++customTreeSelect.index;
        that.config = $.extend({}, that.config, customTreeSelect.config, options);
        that.render();
    };

    //默认配置
    Class.prototype.config = {
        layout: ['province', 'city', 'county', 'street'] //联动层级
            //其他参数
            //……
    };

    //渲染视图
    Class.prototype.render = function() {
        var that = this,
            options = that.config;
        options.elem = $(options.elem),
            othis = $(options.elem),
            hasLayuiRender = othis.next('.layui-form-select'),
            hasRender = othis.next('.' + PNAME);
        // 先取消layui对select的渲染
        hasLayuiRender[0] && hasLayuiRender.remove();
        hasRender[0] && hasRender.remove();
        // console.log(othis);
        othis.after('<div class="layui-text">您已经成功运行了：<strong>layui.regionSelect</strong> 中国省市区选择组件。<br><span style="color: #FF5722;">但是注意：</span>这只是一个扩展组件的示例，暂时没有写相关功能。<br>详见：<a href="https://fly.layui.com/extend/demo/" target="_blank">https://fly.layui.com/extend/demo/</a></div>')
            // options.elem.html();
    }

    //核心入口
    customTreeSelect.render = function(options) {
        var ins = new Class(options);
        return thisIns.call(ins);
    };

    //加载组件所需样式
    layui.link('../lib/customTreeSelect/customTreeSelect.css?v=1', function() {
        //样式加载完毕的回调

    }, 'customTreeSelect'); //此处的“customTreeSelect”要对应 customTreeSelect.css 中的样式： html #layuicss-customTreeSelect{}

    exports('customTreeSelect', customTreeSelect);
});